package linechart;

import common.ChartDemo;
import common.ChartUtil;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.ui.HorizontalAlignment;
import org.jfree.chart.ui.RectangleEdge;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.Dataset;

import java.awt.*;
import java.awt.geom.Ellipse2D;

public class LineChartDemo1 extends ChartDemo {

    public LineChartDemo1(String title) {
        super(title);
    }

    @Override
    protected Dataset createDataset() {
        DefaultCategoryDataset categoryDataset = new DefaultCategoryDataset();
        categoryDataset.addValue(212D, "Classes", "JDK 1.0");
        categoryDataset.addValue(504D, "Classes", "JDK 1.1");
        categoryDataset.addValue(1520D, "Classes", "JDK 1.2");
        categoryDataset.addValue(1842D, "Classes", "JDK 1.3");
        categoryDataset.addValue(2991D, "Classes", "JDK 1.4");
        categoryDataset.addValue(3500D, "Classes", "JDK 1.5");
        return categoryDataset;
    }

    @Override
    protected JFreeChart createChart(Dataset dataset) {
        CategoryDataset categoryDataset = (CategoryDataset) dataset;
        JFreeChart jFreeChart = ChartFactory.createLineChart("Java Standard Class Library", null,
                "Class Count", categoryDataset, PlotOrientation.VERTICAL,
                false, true, false);
        jFreeChart.addSubtitle(new TextTitle("Number of Classes By Release"));
        TextTitle textTitle = new TextTitle("Source: Java In A Nutshell (5th Edition) by David Flanagan (O'Reilly)");
        textTitle.setFont(new Font("SansSerif", 0, 10));
        textTitle.setPosition(RectangleEdge.BOTTOM);
        textTitle.setHorizontalAlignment(HorizontalAlignment.RIGHT);
        jFreeChart.addSubtitle(textTitle);

        CategoryPlot categoryPlot = (CategoryPlot) jFreeChart.getPlot();
        categoryPlot.setRangePannable(true);
        categoryPlot.setRangeGridlinesVisible(false);

        NumberAxis numberAxis = (NumberAxis) categoryPlot.getRangeAxis();
        numberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        ChartUtils.applyCurrentTheme(jFreeChart);

        LineAndShapeRenderer lineAndShapeRenderer = (LineAndShapeRenderer) categoryPlot.getRenderer();
        lineAndShapeRenderer.setDefaultShapesVisible(true);
        lineAndShapeRenderer.setDrawOutlines(true);
        lineAndShapeRenderer.setUseFillPaint(true);
        lineAndShapeRenderer.setDefaultFillPaint(Color.white);
        lineAndShapeRenderer.setSeriesStroke(0, new BasicStroke(3F));
        lineAndShapeRenderer.setSeriesOutlineStroke(0, new BasicStroke(2.0F));
        lineAndShapeRenderer.setSeriesShape(0, new Ellipse2D.Double(-5D, -5D, 10D, 10D));
        return jFreeChart;
    }

    public static void main(String[] args) {
        LineChartDemo1 demo1 = new LineChartDemo1("LineChartDemo1");
        ChartUtil.showChartDemo(demo1);
    }
}
